using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ShopForWeb.Dto.AjaxModels;
using ShopForWeb.Dto.ViewModels;
using ShopForWeb.Service;
using ShopForWeb.UI.Extensions;
using ShopForWeb.UI.Filters;

namespace ShopForWeb.UI.Controllers;
[Authorize]
[Route("shopcart")]
public class ShopCartController : Controller
{
  private ShopCartService _service;
  public ShopCartController(ShopCartService service)
  {
    _service = service;
  }
  [HttpPut("info")]
  public async Task<IActionResult> AddNewAsync(ShopCartViewModel scvm)
  {
    scvm.UserID = this.GetUserID();
    scvm.PushDate = DateTime.Now;
    int result = await _service.AddNewAsync(scvm);
    if (result == 0)
    {
      return Json(new ResponseAjaxModel()
      {
        Status = 1,
        Message = "该商品已在购物车"
      });
    }
    else
    {
      return Json(new ResponseAjaxModel()
      {
        Status = 0,
        Message = "该商品添加完毕！"
      });
    }
  }
  [UserInfoFiler]
  [HttpGet]
  public async Task<IActionResult> IndexAsync()
  {
    int userID = this.GetUserID();
    // int userID = 1;
    var scvms = await _service.GetByUserIDAsync(userID);
    ViewBag.Data = scvms;
    return View();
    // return Json(scvms);
  }
  [HttpDelete("info/{commodityID}")]
  public async Task<IActionResult> Remove(int commodityID)
  {
    int userID = this.GetUserID();
    // int userID = 1;
    int result = await _service.RemoveAsync(userID, commodityID);
    if (result != 0)
      return Json(new ResponseAjaxModel()
      {
        Status = 0,
        Message = "商品移除完毕！"
      });
    else
      return Json(new ResponseAjaxModel()
      {
        Status = 1,
        Message = "商品移除失败！"
      });
  }
}
